<?php

/**
 * This is the model class for table "ykl_gateway".
 *
 * The followings are the available columns in table 'ykl_gateway':
 * @property integer $id
 * @property integer $companyid
 * @property string $gatewayname
 * @property string $address
 * @property string $lng
 * @property string $lat
 */
class Gateway extends CActiveRecord
{
	 var $company_title='';
	 var $len_lat='';
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ykl_gateway';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('companyid, gatewayname, address, provinces,citys', 'required','message'=>'不能为空'),
			array('companyid', 'numerical', 'integerOnly'=>true),
			array('gatewayname, address, lng, lat,privateip,publicip', 'length', 'max'=>128),
			array('gatewayname', 'unique', 'message'=>'路由名称重复'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, companyid, gatewayname, address,privateip,publicip, lng, lat , provinces, citys', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			 'company'=>array(self::BELONGS_TO,'Company','companyid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			  'id' => 'ID',
            'companyid' => '企业ID',
            'gatewayname' => '路由器名',
            'address' => '地址',
            'company_title' =>'企业名称',
			'lng' => '经度',
			'lat' => '纬度',
			'len_lat' => '经纬度',
            'lasttime'=>'最后在线时间',
            'provinces'=>'所属省',
            'provinces'=>'所属市区',
            'publicip'=>'公网IP',
            'privateip'=>'私网IP',

		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('companyid',$this->companyid);
		$criteria->compare('gatewayname',$this->gatewayname,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('provinces',$this->provinces,true);
		$criteria->compare('citys',$this->citys,true);
        $criteria->compare('publicip',$this->publicip,true);
        $criteria->compare('privateip',$this->privateip,true);
		$criteria->compare('lng',$this->lng,true);
		$criteria->compare('lat',$this->lat,true);
		$criteria->compare('company.title',$this->company_title,true);
        $criteria->order="t.id desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Gateway the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
